#!/bin/bash

# 测试商品API
echo "=== 测试商品管理API ==="

# 1. 登录获取token
echo "1. 登录获取token..."
LOGIN_RESPONSE=$(curl -s -X POST "http://localhost:8002/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"123456"}')

echo "登录响应: $LOGIN_RESPONSE"

# 提取token (假设响应格式为 {"token":"xxx"})
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"token":"[^"]*"' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
  echo "登录失败，无法获取token"
  exit 1
fi

echo "获取到token: $TOKEN"

# 2. 使用token访问商品列表API
echo "\n2. 访问商品列表API..."
GOODS_RESPONSE=$(curl -s -X GET "http://localhost:8002/api/v1/goods?pageIndex=1&pageSize=20" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "商品列表响应: $GOODS_RESPONSE"

# 3. 检查响应是否包含数据
if echo "$GOODS_RESPONSE" | grep -q '"code":200'; then
  echo "\n✅ API测试成功！"
else
  echo "\n❌ API测试失败！"
fi